package com.peaksport.pkms.learntest.service;

import java.util.HashMap;
import java.util.Map;

import org.apache.servicecomb.pack.omega.transaction.annotations.Compensable;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.concurrent.ListenableFuture;

import com.peaksport.framework.base.dao.IBaseDAO;
import com.peaksport.framework.base.service.BillBaseService;
import com.peaksport.framework.exception.PKBizException;
import com.peaksport.framework.exception.PKDevException;

@Service
public class TestPackSagaService extends BillBaseService{
	
	private String cseService2  = "cse://pkms-test1/test/pack/saga/testAuditBillEntry?billId={billId}&isException={isException}&isExceptionInCancel={isExceptionInCancel}";


	@Override
	public IBaseDAO<?> getDAO() {
		return null;
	}

	@Compensable(compensationMethod="auditBillHead_cancel")
	@Transactional
	public void auditBillHead(String pk, boolean isException, boolean isExceptionInCancel) {
		String checkSql = "select FBaseStatus from t_tst_billhead where FID='" + pk + "'";
		boolean b = isExistsForNative(checkSql);
		if (!b) throw PKBizException.throwException("不存在记录[%s]", pk);
		String updSql= "update t_tst_billhead set FBaseStatus=99 where FID='" + pk + "'";
		executeSqlForNative(updSql.toString());
		if(isException) throw PKBizException.throwException("审核主表方法auditBillHead异常");
	}
	
	@Transactional
	public void auditBillHead_cancel(String pk, boolean isException, boolean isExceptionInCancel) {
		String updSql= "update t_tst_billhead set FBaseStatus=0 where FID='" + pk + "'and FBaseStatus=99";
		executeSqlForNative(updSql.toString());
		if(isExceptionInCancel) throw PKBizException.throwException("审核主表cancel方法auditBillHead_cancel异常");
	}
	
	
	@SuppressWarnings("unused")
	@Compensable(compensationMethod="auditBillHead2_cancel")
	@Transactional
	public void auditBillHead2(String pk, boolean isException, boolean isExceptionInCancel,
				boolean isException_service2,boolean isExceptionInCancel_service2,
				boolean isException_controller_middle) {
		String checkSql = "select FBaseStatus from t_tst_billhead where FID='" + pk + "'";
		boolean b = isExistsForNative(checkSql);
		if (!b) throw PKBizException.throwException("不存在记录[%s]", pk);
		String updSql= "update t_tst_billhead set FBaseStatus=99 where FID='" + pk + "'";
		executeSqlForNative(updSql.toString());
		if(isException) throw PKBizException.throwException("审核主表方法auditBillHead异常");
		if (isException_controller_middle) throw PKDevException.throwException("在调用service1后service2前发生异常.");
		Map<String,Object> queryMap = new HashMap<>();
		queryMap.put("billId", urlEncode(pk));
		queryMap.put("isException", isException_service2);
		queryMap.put("isExceptionInCancel", isExceptionInCancel_service2);
		
		//String msg = restTemplate.getForObject(cseService2,String.class,queryMap);
		ListenableFuture<ResponseEntity<String>> listenableFuture = asynRestTemplate.exchange(cseService2, HttpMethod.GET,null,String.class,queryMap);
		
	}
	
	@Transactional
	public void auditBillHead2_cancel(String pk, boolean isException, boolean isExceptionInCancel,
			boolean isException_service2,boolean isExceptionInCancel_service2,
			boolean isException_controller_middle) {
		String updSql= "update t_tst_billhead set FBaseStatus=0 where FID='" + pk + "'and FBaseStatus=99";
		executeSqlForNative(updSql.toString());
		if(isExceptionInCancel) throw PKBizException.throwException("审核主表cancel方法auditBillHead_cancel异常");
	}
}
